<?php 

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_contact.php');

if ($_REQUEST['act'] == 'main')
{
    $sql = "SELECT COUNT(*) FROM ecs_contact_list ";
    $count = $db->getOne($sql);
    
    $smarty->assign('ur_here', $_LANG['contact_list']);
    
    $contact_list = contact_list();
    
    $smarty->assign('contact_list',   $contact_list['contacts']);
    $smarty->assign('filter',       $contact_list['filter']);
    $smarty->assign('record_count', $contact_list['record_count']);
    $smarty->assign('page_count',   $contact_list['page_count']);
    $smarty->assign('full_page',    1);
    
    $smarty->display('contact_list.htm');
}
elseif ($_REQUEST['act'] == 'query')
{
    $contact_list = contact_list();
    
    $smarty->assign('contact_list',   $contact_list['contacts']);
    $smarty->assign('filter',       $contact_list['filter']);
    $smarty->assign('record_count', $contact_list['record_count']);
    $smarty->assign('page_count',   $contact_list['page_count']);

    /* 排序标记 */
    /*$sort_flag  = sort_flag($contact_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);*/

    make_json_result($smarty->fetch('contact_list.htm'), '',
        array('filter' => $contact_list['filter'], 'page_count' => $contact_list['page_count']));
}
elseif ($_REQUEST['act'] == 'upload')
{
    $fileName = $_FILES['contact_file']['name'];
    $fileTmpName = $_FILES['contact_file']['tmp_name'];
    
    $targetFile = ROOT_PATH. '/admin/upload/'.$fileName;
    
    $rs = move_upload_file($fileTmpName, $targetFile);
    
    if($rs)
    {
        $cellName = array(
            'Order_Number',
            'Buyer_Name',
            'Buyer_Pay_Name',
            'Buyer_Payment',
            'Buyer_Postage',
            'Buyer_Integral',
            'Total_Amount',
            'Rebates_Integral',
            'Real_Payment',
            'Real_Pay_Integral',
            'Order_Status_Text',
            'Buyer_Message',
            'Consignee',
            'Consignee_Address',
            'Transport_Type',
            'Contact_Number',
            'Contact_Phone',
            'Order_Create_Time',
            'Order_Pay_Time',
            'Treasure_Title',
            'Treasure_Type',
            'Logistics_Number',
            'Logistics_Company',
            'Order_Desc',
            'Treasure_Count',
            'Shop_Id',
            'Shop_Name',
            'Order_Close_Reason',
            'Seller_Service_Charge',
            'Buyer_Service_Charge',
            'Invoice_Title',
            'Is_Phone_Order',
            'Order_Information_Stages',
            'Deposit_List',
            'Modified_SKU',
            'Modified_Address',
            'Exception_Message'
        );
        
        if($_REQUEST['file_type'] == 'csv')
        {
            $handle = fopen($targetFile,"r");
            $row = 0;
            while ($data = fgetcsv($handle)) {
                $row++;
                if($row == 1) continue;
                $num = count($data);
                $str = array();
                for ($c=0; $c < $num; $c++) {
                    $str[] = "'".iconv('GB2312', 'UTF-8',  trim($data[$c], '\''))."'";
                }
                
                $cells = implode(',', $cellName);
                $values   = implode(',', $str);
                
                $sql = "INSERT INTO `ecs_contact_list` ($cells) VALUES ( $values )";
                $db->query($sql);
            }
            fclose($handle);
            
            //$sql = "Load Data InFile '$targetFile' Into Table `ecs_contact_list`";
            //$db->query($sql);
        }
        else
        {
            require_once(ROOT_PATH . '/admin/module/Classes/PHPExcel/IOFactory.php');

            //$phpExcelReader = PHPExcel_IOFactory::createReader();
            $objPHPExcel = PHPExcel_IOFactory::load($targetFile);
            //$objPHPExcel = $phpExcelReader->load($targetFile);

            $objWorkSheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorkSheet->getHighestRow();
            $highestColumn = $objWorkSheet->getHighestColumn();

            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

            for($row=2; $row<$highestRow; $row++)
            {
                $finalCell = array();
                $finalVal  = array();

                for($col=0; $col<$highestColumnIndex; $col++)
                {
                    $val = $objWorkSheet->getCellByColumnAndRow($col, $row)->getValue();
                    $finalCell[] = $cellName[$col];
                    $finalVal[] = "'".ltrim($val, '\'')."'";
                }

                $cells = implode(',', $finalCell);
                $values = implode(',', $finalVal);

                $sql = "INSERT INTO `ecs_contact_list` ($cells) VALUES ( $values )";

                $db->query($sql);
            }
        }
        unset($targetFile);
    }
}
?>